import { _decorator, AudioClip, Component, director, Node } from 'cc';
import { SSS_Consts } from "../Data/SSS_Consts";
import { bdlMgr } from "../Manager/BundleManager_S";
import SSS_GameEventManager, { SS_GameEventData } from "../Manager/SSS_GameEventManager";
import { SSS_AudioManager } from '../Manager/SSS_AudioManager';
import AdManager from '../Manager/AdManager';

const { ccclass, property } = _decorator;

@ccclass('FailPanel')
export default class FailPanel extends Component {
    public static Instance: FailPanel;
    @property(Node)
    public Button_menu: Node | null = null;
    @property(Node)
    public Button_replay: Node | null = null;
    @property(Node)
    public Button_revive: Node | null = null;
    index: number;
    onLoad() {
        FailPanel.Instance = this;
    }
    ShowOn() {
        // this.index = _index;

        this.node.active = true;
        this.AddListener();
        this.Refresh();

        let audio_root = `audio/fail`;
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
    }
    AddListener() {
        this.Button_menu.on(Node.EventType.TOUCH_END, this.On_Button_menu, this);
        this.Button_replay.on(Node.EventType.TOUCH_END, this.On_Button_replay, this);
        this.Button_revive.on(Node.EventType.TOUCH_END, this.On_Button_revive, this);
    }
    RemoveListener() {
        this.Button_menu.off(Node.EventType.TOUCH_END, this.On_Button_menu, this);
        this.Button_replay.off(Node.EventType.TOUCH_END, this.On_Button_replay, this);
        this.Button_revive.off(Node.EventType.TOUCH_END, this.On_Button_revive, this);
    }
    On_Button_menu() {
        // console.log(`On_Button_menu`);

        this.ShowOff();
        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Menu);
        SSS_AudioManager.PlayButtonAudio();
    }
    
    On_Button_replay() {
        // console.log(`On_Button_replay`);

        this.ShowOff();
        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Replay);
        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_revive() {
        console.log(`On_Button_revive------------`);

        AdManager.是否看完广告 = false;
        AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
            () => {
                director.emit(SS_GameEventData.广告结束);
                console.log("复活成功！");

                let audio_root = `audio/life received`;
                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                    SSS_AudioManager.Instance.playSFX(res, false);
                });

                this.ShowOff();
                SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Revive);
                SSS_AudioManager.PlayButtonAudio();

                // this.ShowOff();
            },
            () => {
                director.emit(SS_GameEventData.广告结束);

                console.log("复活失败！");
            },
        );

    }
}